package patrol.mobile.com.mobilepatrol.activity.security.emergency;

import android.text.Editable;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;

import com.dl7.recycler.adapter.BaseQuickAdapter;
import com.dl7.recycler.listener.OnRequestDataListener;
import com.google.gson.JsonArray;
import com.google.gson.JsonObject;
import com.google.gson.JsonParser;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;

import okhttp3.Call;
import patrol.mobile.com.mobilepatrol.R;
import patrol.mobile.com.mobilepatrol.activity.CommonListActivity;
import patrol.mobile.com.mobilepatrol.activity.security.ConsTitles;
import patrol.mobile.com.mobilepatrol.activity.security.adapter.EmergencyOutSaveManager1ListAdapter;
import patrol.mobile.com.mobilepatrol.api.Urls;
import patrol.mobile.com.mobilepatrol.bean.DateBean;
import patrol.mobile.com.mobilepatrol.bean.OutSaveManagerBean1;
import patrol.mobile.com.mobilepatrol.utils.GsonUtil;
import patrol.mobile.com.mobilepatrol.utils.TimeUtils;
import patrol.mobile.com.mobilepatrol.utils.net.HttpCallBack;
import patrol.mobile.com.mobilepatrol.utils.net.HttpSender;
import patrol.mobile.com.mobilepatrol.view.CallBack;
import patrol.mobile.com.mobilepatrol.widget.MySpinner;

/**
 * Created by K on 2018/9/14.
 */
public class EmergencyOutSaveManager1Activity extends CommonListActivity implements CallBack<DateBean> {
    private View itemTopView;
    private EditText etOrgName;
    private MySpinner spStartTime;
    private ImageView ivSearch;
    private int page = 1;
    private String orgName = "";
    private String year = "";
    private SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM");

    @Override
    public void initView() {
        super.initView();
        itemTopView = LayoutInflater.from(this).inflate(R.layout.activity_emergency_out_save_manager, null);
        etOrgName = itemTopView.findViewById(R.id.et_orgName);
        spStartTime = itemTopView.findViewById(R.id.spStartTime);
        ivSearch = itemTopView.findViewById(R.id.iv_search);
        flContent.addView(itemTopView);
        year = TimeUtils.getYearList().get(0);
    }


    @Override
    public void initTitle() {
        super.initTitle();
    }

    @Override
    protected BaseQuickAdapter initAdapter() {
        return new EmergencyOutSaveManager1ListAdapter(this);
    }

    @Override
    protected String initTitleName() {
        return ConsTitles.EMERGENCY_OUT_SAVE_Manager_1;
    }

    @Override
    public void configAdapter() {
        super.configAdapter();

        mAdapter.setRequestDataListener(new OnRequestDataListener() {
            @Override
            public void onLoadMore() {
                requestData(false);
            }
        });
    }

    @Override
    public void setListener() {
        super.setListener();
        spStartTime.getmText().setText(sdf.format(new Date()));
        spStartTime.setmDatas(TimeUtils.getYearList(), new MySpinner.CallBack() {
            @Override
            public void onSelect(int position) {
                spStartTime.getmText().setText(TimeUtils.getYearList().get(position));
                year = TimeUtils.getYearList().get(position);
                requestData(true);
            }
        });
//        mAdapter.setOnItemClickListener(new OnRecyclerViewItemClickListener() {
//            @Override
//            public void onItemClick(View view, int position) {
//                Intent intent = new Intent(EmergencyOutSaveManager1Activity.this,EmergencyOutSaveManager2Activity.class);
//                intent.putExtra("id",)
//                startActivity(EmergencyOutSaveManager2Activity.class);
//            }
//        });


//        spStartTime.setOnClickListener(new View.OnClickListener() {
//            @Override
//            public void onClick(View view) {
//                DatePickerUtil.getInstance(EmergencyOutSaveManager1Activity.this).setCallBack(EmergencyOutSaveManager1Activity.this);
//            }
//        });
        requestData(true);
        etOrgName.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {

            }

            @Override
            public void afterTextChanged(Editable s) {
                requestData(true);
            }
        });
        ivSearch.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                requestData(true);
            }
        });
    }

    private void requestData(boolean isRefresh) {
        if (isRefresh) {
            page = 1;
        } else {
            page++;
        }
        HttpSender sender = new HttpSender(this, baseUrl, Urls.IEMERGENCY_OUT_SAVE_MANAGER_1);
        HashMap<String, String> map = new HashMap<>();
        map.put("orgName", TextUtils.isEmpty(etOrgName.getText().toString()) ? "" : etOrgName.getText().toString());
        map.put("year", year);
        map.put("page", page + "");
        map.put("rows", ROWS + "");
        sender.requestPost(map, new HttpCallBack(sender) {
            @Override
            public void onError(String code, Call call, Exception e) {

            }

            @Override
            public void onResponse(String code, String response) {
                JsonParser jp = new JsonParser();
                JsonObject jo = jp.parse(response).getAsJsonObject();

                JsonArray listArray = jo.getAsJsonArray("list");
                int rows = jo.get("rows").getAsInt();
                List<OutSaveManagerBean1> outSaveManagerBean1s = GsonUtil.jsonToList(listArray.toString(), OutSaveManagerBean1.class);
                setData(page, false, outSaveManagerBean1s, rows);
            }
        });
    }


    @Override
    public void onBack(DateBean dateBean) {
        spStartTime.getmText().setText(dateBean.getmYear() + "-" + dateBean.getmMonth());
        year = dateBean.getmYear() + "";
        requestData(true);
    }
}
